<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <!-- 请选择开始时间
  <a-date-picker v-model="starttime"  :format="dateFormat" />
    请选择结束时间
   <a-date-picker v-model="endtime"   :format="dateFormat" /> -->
    <a-table
      :columns="columns"
      :data-source="data"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:'radio'}"
      :pagination="ipagination">
      <template #name="{text}">
        <a>{{ text }}</a>
      </template>
    </a-table>


  </a-modal>
</template>

<script>
  const columns = [
    {
      title: '序号',
      dataIndex: '',
      key: 'rowIndex',
      width: 60,
      align: "center",
      customRender: function (t, r, index) {
        return parseInt(index) + 1;
      }
    },
    {
      title: '用户名称',
      dataIndex: 'companyName',
      key: 'age',
      width: 80,
    },
    {
      title: '用户编号',
      dataIndex: 'id',
      key: 'address 1',
      ellipsis: true,
    },
    {
      title: '用户类别',
      align: "center",
      dataIndex: 'supplierIdentity',
      customRender: (supplierIdentity) => {
        //字典值替换通用方法
        if (supplierIdentity == 0)
          return "供应商";
        else
          return "加工厂";
      }
    },

  ];

  const data = [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
      tags: ['nice', 'developer'],
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 2 Lake Park, London No. 2 Lake Park',
      tags: ['loser'],
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park, Sidney No. 1 Lake Park',
      tags: ['cool', 'teacher'],
    },


  ];
  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "paidanSupplierList",
    data() {
      return {
        sysuserId: '',
        orderId: '',
        orderSn: '',
        orderType: 0,
        dateFormat: 'YYYY-MM-DD',
        starttime: '',
        endtime: '',
        /* 分页参数 */
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        ids: [],
        /* table选中keys*/
        selectedRowKeys: [],
        /* table选中record*/
        selectionRows: [],
        data,
        columns,
        title: "操作",
        visible: false,
        xianshi: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          list: "/Supplier/list",
          add: "/orderDistributeLeaflet/distributeOrdertoSysuser",
          edit: "/orderDistributeLeaflet/distributeOrdertoSysuser",
        },
      }
    },
    created() {

      this.getpeopleList();

    },
    methods: {

      onSelectChange(selectedRowKeys, selectionRows) {
        this.ids = [];
        this.selectedRowKeys = selectedRowKeys;

        this.selectionRows = selectionRows;
        for (var i = 0; i < this.selectionRows.length; i++) {
          this.ids.push(this.selectionRows[i].id);
          console.log("看一看能不能成功" + this.ids[i]);
        }
        this.sysuserId = this.ids[0];
      },

      getpeopleList() {
        getAction(this.url.list).then((res) => {
          if (res.success) {
            this.data = res.result.records;
            /*this.$message.success(res.message);*/
            this.$emit('ok');
          } else {
            this.$message.warning(res.message);
          }
        })
      },
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = {...this.pagination};
        pager.current = pagination.current;
        this.pagination = pager;
        this.fetch({
          results: pagination.pageSize,
          page: pagination.current,
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...filters,
        });
      },
      add() {
        this.edit({});
      },
      edit(record, ordertype) {
        this.orderId = record.id;
        this.orderSn = record.orderSn;
        this.orderType = ordertype;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'name', 'keyWord', 'sex', 'age', 'email', 'content'))
          //时间格式化
          this.form.setFieldsValue({punchTime: this.model.punchTime ? moment(this.model.punchTime, 'YYYY-MM-DD HH:mm:ss') : null})
          this.form.setFieldsValue({birthday: this.model.birthday ? moment(this.model.birthday) : null})
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'post';
            }
            let formData = Object.assign(this.model, values);
            // this.starttime=this.starttime.substring(0,10);
            //  this.endtime=this.endtime.substring(0,10);
            formData['sysuserId'] = this.sysuserId;
            formData['orderId'] = this.orderId;
            formData['orderSn'] = this.orderSn;
            formData['orderType'] = this.orderType;

            //时间格式化
            formData.starttime = formData.starttime ? formData.starttime.format('YYYY-MM-DD') : null;
            formData.endtime = formData.endtime ? formData.endtime.format('YYYY-MM-DD') : null;
            this.starttime = this.starttime ? this.starttime.format('YYYY-MM-DD') : null;
            this.endtime = this.endtime ? this.endtime.format('YYYY-MM-DD') : null;
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$parent.loadData()
                this.ids = [];
                this.selectedRowKeys = [];
                this.selectionRows = [];
              } else {

                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>